package com.android.phoenix.fragments.userinfoimprove;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnScrollListener;
import android.widget.NumberPicker.OnValueChangeListener;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;
import com.android.phoenix.fragments.FragmentSwitchListener;

public class UserInfoWeightFragment extends Fragment implements
		OnClickListener, OnValueChangeListener, OnScrollListener {
	/** 事件监听 */
	private FragmentSwitchListener listener;

	private Button btnUserInfoWeightLast = null;
	private Button btnUserInfoStartSport = null;
	private NumberPicker numpUserWeight = null;

	private int userWeight = 60;

	@Override
	public void onAttach(Activity activity) {
		try {
			listener = (FragmentSwitchListener) activity;
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fra_userinfoweight, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		initViews();
	}

	/**
	 * @Title: initViews
	 * @Description: 控件初始化
	 * @author: WanChao
	 * @date: 2015-1-7 下午10:26:33
	 */
	private void initViews() {
		numpUserWeight = (NumberPicker) getActivity().findViewById(
				R.id.numpUserWeight);
		numpUserWeight.setMaxValue(100);
		numpUserWeight.setMinValue(20);
		numpUserWeight.setValue(60);
		numpUserWeight.getChildAt(0).setFocusable(false);
		numpUserWeight.setOrientation(NumberPicker.HORIZONTAL);
		numpUserWeight.setOnValueChangedListener(this);
		numpUserWeight.setOnScrollListener(this);

		btnUserInfoWeightLast = (Button) getActivity().findViewById(
				R.id.btnUserInfoWeightLast);
		btnUserInfoStartSport = (Button) getActivity().findViewById(
				R.id.btnUserInfoStartSport);
		btnUserInfoWeightLast.setOnClickListener(this);
		btnUserInfoStartSport.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnUserInfoWeightLast:
			listener.fragmentSwitch(UserInfoWeightFragment.class.getName(),
					UserInfoHeightFragment.class.getName(), null);
			break;
		case R.id.btnUserInfoStartSport:
			saveUserWeight();
			listener.fragmentSwitch(UserInfoWeightFragment.class.getName(),
					FragmentSwitchListener.ACTIVITY_FINISH, null);
			break;
		}
	}

	private void saveUserWeight() {
		MainApplication.mSharedPreferences.edit()
				.putInt(Constant.USERWEIGHT, userWeight).commit();
		MainApplication.mSharedPreferences.edit()
				.putBoolean(Constant.ISFIRSTLOGIN, false).commit();
	}

	@Override
	public void onScrollStateChange(NumberPicker arg0, int scrollState) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onValueChange(NumberPicker arg0, int oldValue, int newValue) {
		// TODO Auto-generated method stub
		userWeight = newValue;
	}
}
